<?php
/**
 *@fileoverview: [群博客] HTML页面常用数据整理工具
 *@author: 辛少普 <shaopu@staff.sina.com.cn>
 *@date: Mon Dec 06 01:35:39 GMT 2010
 *@copyright: sina
 */

    class ml_biz_htmlmaker
    {
        const NL = "\n";
        private $_meta = array();
        private $_page_title;
        private $_css = array();
        private $_css_i = 0;
        private $_body;
        private $_js = array();
        
        public function __construct(){}
        
        public function add_meta($key , $data)
        {
            $this->_meta[$key] = $data;
            return ;
        }
        public function get_meta()
        {
            foreach ($this->_meta as $key => $row)
                $meta .= '<meta name="'.$key.'" content="'.$row.'" />'.self::NL ;
            return $meta;
        }
        
        public function set_page_title($title)
        {
            $this->_page_title = $title;
            return ;
        }
        public function get_page_title()
        {
            return $this->_page_title;
        }
        
        public function add_import_css($url , $id = '' , $sort = 0)
        {
            $this->_css[] = array(
                'type' => 'i',
                'id' => $id,
                'url' => $url
            );
            return ;
        }
        public function add_output_css($data , $id = '' , $sort = 0)
        {
            $this->_css[$sort.'_'.$this->_css_i] = array(
                'type' => 'o',
                'id' => $id,
                'data' => $data
            );
            $this->_css_i++;
            return ;
        }
        public function get_css()
        {
            ksort($this->_css);
            foreach ($this->_css as $row)
            {
                $id_str = $row['id']?' id='.$row['id']:'';
                
                if($row['type'] == 'o')
                    $css .= '<style type="text/css"'.$id_str.'>'.$row['data'].'</style>'.self::NL ;
                else if($row['type'] == 'i')
                {
                    
                    $css .= '<link href="'.$row['url'].'" type="text/css" rel="stylesheet"'.$id_str.' />'.self::NL ;
                }
            }
            return $css;
        }
        
        
        public function add_import_js($url , $position = 'bottom')
        {
            $this->_js[$position][] = array(
                'type' => 'i',
                'url' => $url
            );
            return ;
        }
        public function add_output_js($data , $position = 'bottom')
        {
            $this->_js[$position][] = array(
                'type' => 'o',
                'data' => $data
            );
            return ;
        }
        public function get_js()
        {
            foreach ($this->_js as $pos => $data)
            {
                foreach ($data as $row)
                {
                    $id_str = $row['id']?' id='.$row['id']:'';
                    
                    if($row['type'] == 'o')
                        $js[$pos] .= '<script type="text/javascript"'.$id_str.'>'.$row['data'].'</script>'.self::NL ;
                    else if($row['type'] == 'i')
                        $js[$pos] .= '<script type="text/javascript" src="'.$row['url'].'"'.$id_str.'></script>'.self::NL ;
                }
            }
            return $js;
        }

    }
?>